package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

//    service不能进行数据库的操作，所以需要调用mapper

    @Autowired
    private DeptMapper deptMapper;
//    调用部门管理的mapper接口

    @Autowired
    private EmpMapper empMapper;


    /*
    * 查询所有数据
    * */
    @Override
    public List<Dept> list() {
        return deptMapper.list();
//        调用mapper的方法来实现查询所有数据
    }

    /*
    * 删除数据
    * */
    @Override
    public void delete(Integer id) {
        deptMapper.deleteById(id);
        //根据ID删除部门


        //根据部门的id，删除部门下的员工
        empMapper.deleteById(id);

    }

    @Override
    public void add(Dept dept) {
        //补全属性，
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.add(dept);
    }

    /*
    * 根据id查询数据
    * */
    @Override
    public Emp getById(Integer id) {
        return deptMapper.getById(id);

    }

    /*
    * 修改数据
    * */
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());


        deptMapper.update(dept);
    }


//    deptService的实现类，
}
